package com.admission.web.admin;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Message;
import com.admission.service.MessageManager;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;

@Namespace("/admin")
public class MessageAdminAction extends CrudActionSupport<Message> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private String ids;
	private MessageManager messageManager;
	private Page<Message> page = new Page<Message>(PageUtils.DEFAULT_PAGE_SIZE);
	private List<Message> messageList;
	private String status; // 留言开闭状态
	private Message entity;
	private String replyStatus; // 判断回复状态
	private String tag; // 精品发布属性
	private String province = "";// 省份
	private String type = "";// 科类

	public Message getEntity() {
		return entity;
	}

	public void setEntity(Message entity) {
		this.entity = entity;
	}

	public Message getModel() {
		return entity;
	}

	/**
	 * 留言列表
	 */
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if (replyStatus.equals("0")) {
			PropertyFilter p1 = new PropertyFilter("EQS_replyTag",
					Message.REPLYFALSE);
			filters.add(p1);
		}
		if (replyStatus.equals("1")) {
			PropertyFilter p2 = new PropertyFilter("EQS_replyTag",
					Message.REPLYTRUE);
			filters.add(p2);
		}
		if (replyStatus.equals("2")) {
			PropertyFilter p3 = new PropertyFilter("EQS_tag", Message.QUALITY);
			filters.add(p3);
		}
		if (province != "") {
			PropertyFilter p4 = new PropertyFilter("LIKES_comeFrom", province);
			filters.add(p4);
		}
		if (type != "") {
			PropertyFilter p5 = new PropertyFilter("LIKES_type", type);
			filters.add(p5);
		}
		page.setOrder(Page.DESC);
		page.setOrderBy("editTime");
		messageManager.pageByFilter(page, filters);
		messageList = page.getResult();
		return SUCCESS;
	}

	/**
	 * 获取唯一回复
	 */
	@Override
	public String input() throws Exception {
		return INPUT;
	}

	/**
	 * 改变留言开放状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception {
		messageManager.changeConsult(status);
		if (status.equals("1"))
			Struts2Utils.renderText("true");
		else
			Struts2Utils.renderText("false");
		return null;
	}

	/**
	 * 保存回复信息
	 */
	@Override
	public String save() throws Exception {
		try {
			messageManager.save(entity);
		} catch (Exception e) {
			this.addActionMessage("操作失败，请重新操作");
		}
		return list();
	}

	/**
	 * 根据id删除
	 */
	@Override
	public String delete() throws Exception {
		messageManager.deleteByIds(ids);
		if (StringUtils.isEmpty(replyStatus)) {
			Struts2Utils.renderText("true");
			return null;
		}
		return list();
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new Message();
		} else
			entity = messageManager.getById(id);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public MessageManager getMessageManager() {
		return messageManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	public Page<Message> getPage() {
		return page;
	}

	public void setPage(Page<Message> page) {
		this.page = page;
	}

	public List<Message> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getReplyStatus() {
		return replyStatus;
	}

	public void setReplyStatus(String replyStatus) {
		this.replyStatus = replyStatus;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
